#include "StdAfx.h"
#include "MutualWorker.h"


static UINT __cdecl MutualWorkerProc(LPVOID pParameter);


IMPLEMENT_DYNAMIC(CMutualWorker, CObject)


CMutualWorker::CMutualWorker(CMutualWorkerController &a_rController, CEvent &a_revOwn, CEvent &a_revOther) :
m_revOwn(a_revOwn),
m_revOther(a_revOther),
m_rController(a_rController)
{
}


VOID CMutualWorker::Switch()
{
	VERIFY(m_revOther.SetEvent());
	VERIFY(m_revOwn.Lock());
}


CWinThread *CMutualWorker::Start()
{
	return AfxBeginThread(MutualWorkerProc, (LPVOID)this);
}


UINT __cdecl MutualWorkerProc(LPVOID pParameter)
{
	CMutualWorker *pThis = (CMutualWorker*)pParameter;
	ASSERT_VALID(pThis);

	try
	{
		pThis->Run();
	}
	catch (BOOL bSuccess)
	{
		return bSuccess ? 0 : 1;
	}

	return 0;
}
